class MapModel extends Laya.EventDispatcher{

    /**
     * 数据变更
     */
    public static INFO_CHANGE:string = "INFO_CHANGE";

    /**
     * 历史记录变更
     */
    public static HISTORY_INFO_CHANGE:string = "HISTORY_INFO_CHANGE";

    private static _instance:MapModel;//数据

    public addressList:Array<string>;//所有土地拥有者地址列表
    public ownIdList:Array<number>;//自己土地拥有id列表
    public newOwnID:number = -1;//自己最新购买的领土
    public occupyIdList:Array<number>;//占领的id列表
    public lastWinAddress:string;//上一局获胜者地址
    public lastWinIndex:number;//上一局获胜土地编号
    public nowCount:number = 0;//获取当前局数
    public occupyNum:number = 0;//占领数量
    public historyList:Array<LogVo>;//历史记录

    constructor(){
        super();
        //帐号侦听
        Web3Manager.getInstance().on(Web3Manager.ACCOUNT_CHANGE,this,this.accountChangeHandler);
    }

    /**
     * 获取数据单例
     */
    public static getInstance():MapModel{
        if(MapModel._instance == null){
            MapModel._instance = new MapModel();
        }
        return MapModel._instance;
    }

    /**
     * 计时器开始执行合约
     */
    public startExecContract():void{
        Laya.timer.clear(this,this.reqMapInfo);
        Laya.timer.loop(10000,this,this.reqMapInfo);
        this.reqMapInfo();
    }

    /**
     * 停止计时器
     */
    public stopExecContract():void{
        Laya.timer.clear(Web3Manager,this.reqMapInfo);
    }

    /**
     * 购买地图
     */
    public reqBuyMapById(id:number):void{
        Web3Manager.execContract(Web3Manager.reqBuy,[id],MapModel.recBuy);
    }

    /**
     * 请求历史记录信息
     */
    public reqHistory(start:number,num:number):void{
        start = start <= 0 ? 1 : start;
        var end:number = start + num - 1;
        var maxCount:number = this.nowCount - 1;
        end = end > maxCount ? maxCount : end;
        Web3Manager.execContract(Web3Manager.reqHistory,[start,end],MapModel.recHistory);
    }

    //请求地图信息
    private reqMapInfo():void{
        //检查网络
        if(Web3Manager.nowNet != Global.NEED_NET){
            return;
        }
        //查询土地归属列表
        Web3Manager.execContract(Web3Manager.reqOwnInfo,null,MapModel.recOwnInfo);
        //查询上一局的胜利者
        Web3Manager.execContract(Web3Manager.reqLastWin,null,MapModel.recLastWin);
        //查询当前局数
        Web3Manager.execContract(Web3Manager.reqCount,null,MapModel.recCount);
    }

    //回复购买
    private static recBuy(value:string,type:string):void{
        if(type == "suc"){
            Alert.getInstance().showMsg = LangUtils.getText(11);
            Alert.getInstance().open();
            //添加到计时里面
            MapByAlert.getInstance().addBuyId();
        }
    }

    //回复土地归属信息
    private static recOwnInfo(value:Array<string>,type:string):void{
        if(type == "suc"){
            var model:MapModel = MapModel.getInstance();
            model.addressList = value;
            //检测自己的 和占领的
            model.occupyNum = 0;
            var tempOwnIdList:Array<number> = [];
            model.occupyIdList = [];
            var address:string;
            for(var i:number=0;i<model.addressList.length;i++){
                address = model.addressList[i];
                if(Web3Manager.defaultAccount == address){
                    tempOwnIdList.push(i);
                }
                if(address != Web3Manager.EMPTY_ACCOUNT){
                    model.occupyNum++;
                    model.occupyIdList.push(i);
                }
            }
            if(model.ownIdList && model.ownIdList.toString() != tempOwnIdList.toString()){
                for(i=0;i<tempOwnIdList.length;i++){
                    if(model.ownIdList.indexOf(tempOwnIdList[i]) == -1){
                        model.newOwnID = tempOwnIdList[i];
                    }
                }
            }
            model.ownIdList = tempOwnIdList;
            //分派事件
            MapModel._instance.event(MapModel.INFO_CHANGE);
        }
    }

    //回复上一局获胜者
    private static recLastWin(value:string,type:string):void{
        if(type == "suc"){
            var model:MapModel = MapModel.getInstance();
            model.lastWinAddress = value[0];
            model.lastWinIndex = Number(value[1]["c"][0]);
            //分派事件
            MapModel._instance.event(MapModel.INFO_CHANGE);
        }
    }

    //回复当前局数
    private static recCount(value:string,type:string):void{
        if(type == "suc"){
            var model:MapModel = MapModel.getInstance();
            model.nowCount = Number(value);
            //分派事件
            MapModel._instance.event(MapModel.INFO_CHANGE);
        }
    }

    //回复历史记录信息
    private static recHistory(value:Array<any>,type:string):void{
        if(type == "suc"){
            var model:MapModel = MapModel.getInstance();
            var addressList:Array<string> = value[0];
            var idList:Array<number> = [];
            for(var i:number=0;i<addressList.length;i++){
                idList.push(value[1][i]["c"][0]);
            }
            addressList = addressList.reverse();
            idList = idList.reverse();
            model.historyList = [];
            var logVo:LogVo;
            var historyIndex = addressList.length;//Number(value[2]["c"][0]);
            for(var i:number=0;i<addressList.length;i++){
                logVo = new LogVo();
                logVo.address = addressList[i];
                logVo.count = historyIndex--;
                logVo.mapIdex = idList[i] + 1;
                model.historyList.push(logVo);
            }
            //分派事件
            MapModel._instance.event(MapModel.HISTORY_INFO_CHANGE);
        }
    }

    //帐号信息变更
    private accountChangeHandler():void{
        var model:MapModel = MapModel.getInstance();
        if(model.addressList == null){
            return;
        }
        model.ownIdList = [];
        var address:string;
        for(var i:number=0;i<model.addressList.length;i++){
            address = model.addressList[i];
            if(Web3Manager.defaultAccount == address){
                model.ownIdList.push(i);
            }
        }
        //分派事件
        MapModel._instance.event(MapModel.INFO_CHANGE);
    }
}
